package com.xyzh05.webapp.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Preparable;
import com.xyzh05.model.Role;
import com.xyzh05.model.User;

public class RoleAction extends BaseAction implements Preparable {
	private static final long serialVersionUID = 3006285032409439491L;
	
    private List<Role> roleList = new ArrayList<Role>();
    private List<Long> userIds = new ArrayList<Long>();
    private List<Long> deptIds = new ArrayList<Long>();
    private Role role;
    private User user;
    private Long id;
    private Long deptId;
    private Long userId;
    private Short status;
    private String query;
    private Integer resultCode;
    private Integer actionType;

    public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Long> getDeptIds() {
		return deptIds;
	}

	public void setDeptIds(List<Long> deptIds) {
		this.deptIds = deptIds;
	}

	public Short getStatus() {
		return status;
	}

	public void setStatus(Short status) {
		this.status = status;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Long> getUserIds() {
		return userIds;
	}

	public void setUserIds(List<Long> userIds) {
		this.userIds = userIds;
	}

	public Integer getResultCode() {
		return resultCode;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	/**
     * Grab the entity from the database before populating with request parameters
     */
    public void prepare() {
        if (getRequest().getMethod().equalsIgnoreCase("post")) {
            // prevent failures on new
            String roleId = getRequest().getParameter("role.id");
            if (roleId != null && !roleId.equals("")) {
                role = roleManager.get(new Long(roleId));
            }
        }
    }

    public void setQ(String q) {
        this.query = q;
    }

    public String list() {
    	roleList = roleManager.search(query, Role.class);
        return SUCCESS;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public Integer getActionType() {
		return actionType;
	}

	public void setActionType(Integer actionType) {
		this.actionType = actionType;
	}

	public Long getId() {
		return id;
	}

	public String delete() {
        roleManager.remove(role.getId());
        saveMessage(getText("role.deleted"));

        return SUCCESS;
    }

    public String edit() {
        if (id != null) {
            role = roleManager.get(id);
        } else {
            role = new Role();
        }

        return SUCCESS;
    }

    public String save() throws Exception {
        if (cancel != null) {
            return "cancel";
        }

        if (delete != null) {
            return delete();
        }

        boolean isNew = (role.getId() == null);

        roleManager.save(role);

        String key = (isNew) ? "role.added" : "role.updated";
        saveMessage(getText(key));

        if (!isNew) {
            return INPUT;
        } else {
            return SUCCESS;
        }
    }
    
}